package com.github.data_manage.dao

import com.github.data_manage.domain.po.ContrastReturnData
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDate
import java.time.LocalDateTime

/**
 *@author CHN
 *@since 2024-08-10 14:48
 */
interface ContrastReturnDataDao : BaseDao<ContrastReturnData> {

    @Query("select f from ContrastReturnData f where f.contrastId = :contrastId and f.time <= :lastMonth order by f.time desc limit 1")
    fun findLastNavDataByContrastId(
        @Param("contrastId") contrastId: Int,
        @Param("lastMonth") lastMonthEndTime: LocalDateTime
    ): ContrastReturnData?

    @Query("select f from ContrastReturnData f where f.contrastId = :contrastId and f.time = :lastMonth order by f.time desc limit 1")
    fun findByContrastIdAndTime(contrastId: Int, @Param("lastMonth") lastMonthEndTime: LocalDate): ContrastReturnData?

    fun findAllByContrastId(contrastId: Int): List<ContrastReturnData>

    fun findAllByContrastIdAndTimeLessThan(contrastId: Int, time: LocalDate): List<ContrastReturnData>

    @Modifying
    @Transactional
    fun deleteAllByContrastId(contrastId: Int)

    fun findAllByContrastIdIn(contrastIds: List<Int>): List<ContrastReturnData>

    fun findAllByContrastIdIsAndTimeBetween(
        contrastId: Int,
        startTime: LocalDate,
        endTime: LocalDate
    ): List<ContrastReturnData>


}
